package com.lykj.llw.box.ui.activity.agent

import android.content.Intent
import android.os.Bundle
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.R
import com.lykj.llw.box.data.bean.response.agent.AgentUserInfoResponse
import com.lykj.llw.box.databinding.ActAgentWithdrawalBinding
import com.lykj.llw.box.textwatcher.DecimalInputTextWatcher
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 代理提现页面
 * ================================================
 *
 * @author gosha
 * @date 2023/10/30-15:04
 */
class AgentWithdrawalActivity : BaseActivity() {
    lateinit var binding: ActAgentWithdrawalBinding

    private var mTextWatcher: DecimalInputTextWatcher? = null
    private var mAmount = 0.0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActAgentWithdrawalBinding.inflate(layoutInflater)

        setContentView(binding.root)
        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this)
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initUI() {
        mAmount = intent.getDoubleExtra("amount", 0.0)
        binding.tvWithdrawalAmount.text = "$mAmount"

        binding.titleBar.root.setBackgroundColor(ColorUtils.getColor(R.color.grey_FFFAFAFA))
        binding.titleBar.tvTitle.text = "提现"
        binding.titleBar.tvRightOption.text = "提现记录"
    }

    //发起提现申请
    private fun withdrawApply() {
        clearBodyParam()
        initBodyParam()
        bodyParams["money"] = binding.etWithdrawalAmount.text.toString()
        RetrofitManager.instance.withdrawApply(bodyParams,
            object : CustomResponseBodyObserver<ResponseBody>() {
                override fun onSubscribe(d: Disposable) {
                    addDisposables(this@AgentWithdrawalActivity, d)
                }

                override fun specificHandle(responseBody: String) {
                    //刷新提现信息
                    obtainAgentUserInfo()
                    ToastUtils.showShort(this@AgentWithdrawalActivity, "提现申请提交成功")
                }

                override fun handleException(ret: String, msg: String?) {
                    msg?.let {
                        ToastUtils.showShort(this@AgentWithdrawalActivity, it)
                    }
                }
            })
    }

    //获取代理用户信息
    private fun obtainAgentUserInfo() {
        RetrofitManager.instance.obtainAgentInfo(object :
            CustomResponseBodyObserver<ResponseBody>() {
            override fun onSubscribe(d: Disposable) {
                addDisposables(this@AgentWithdrawalActivity, d)
            }

            override fun specificHandle(responseBody: String) {
                val bean = GsonUtils.fromJson(responseBody, AgentUserInfoResponse::class.java)

                binding.tvWithdrawalAmount.text = "${bean.data.money}"
            }

            override fun handleException(ret: String, msg: String?) {
                msg?.let {
                    ToastUtils.showShort(this@AgentWithdrawalActivity, it)
                }
            }
        })
    }

    override fun initData() {
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        binding.titleBar.tvRightOption.clickWithTrigger {
            startActivity(Intent(this, AgentWithdrawalRecordActivity::class.java))
        }

        binding.tvAllWithdrawal.clickWithTrigger(350) {
            binding.etWithdrawalAmount.setText("${binding.tvWithdrawalAmount.text}")
        }

        binding.tvBtnApplyWithdrawal.clickWithTrigger(300) {
            if (binding.tvAllWithdrawal.text.isNullOrBlank()) {
                ToastUtils.showShort(this@AgentWithdrawalActivity, "请输入正确的提现金额")
                return@clickWithTrigger
            }
            withdrawApply()
        }

        mTextWatcher = DecimalInputTextWatcher(binding.etWithdrawalAmount, 9, 2)
        binding.etWithdrawalAmount.addTextChangedListener(mTextWatcher)

        KeyboardUtils.registerSoftInputChangedListener(this) {
            if (it == 0) {
                binding.etWithdrawalAmount.clearFocus()
            }
        }
    }

    override fun onDestroy() {
        binding.etWithdrawalAmount.removeTextChangedListener(mTextWatcher)
        removeDisposables(this)
        super.onDestroy()
    }
}